#include "lock.h"

Lock::Lock(void)
{
#ifdef WIN32
	InitializeCriticalSection( &m_pSection );
#else
	pthread_mutex_init(&m_pSection, NULL);
#endif
}

Lock::~Lock(void)
{
#ifdef WIN32
    DeleteCriticalSection( &m_pSection );
#else
	int nError = pthread_mutex_destroy( &m_pSection );
	if ( nError != 0 )
	{
		printf("pthread_mutex_destroy error : %d\n", nError );
	}
#endif
}

void Lock::Lockit()
{
#ifdef WIN32
	EnterCriticalSection( &m_pSection );
#else
	int nError = pthread_mutex_lock( &m_pSection );
	if ( nError != 0 )
	{
		printf("pthread_mutex_lock error : %d\n", nError );
	}
#endif
}

void Lock::UnLock()
{
#ifdef WIN32
    LeaveCriticalSection( &m_pSection );
#else
	int nError = pthread_mutex_unlock( &m_pSection );
	if ( nError != 0 )
	{
		printf("pthread_mutex_unlock error : %d\n", nError );
	}
#endif
}

///////////////////////////////////////////////////////////////////////////////

CAutoLock::CAutoLock(Lock *pLock)
{
	m_pLock = pLock;
	m_pLock->Lockit();
}

CAutoLock::~CAutoLock()
{
	Destroy();
}

void CAutoLock::Destroy()
{
	if (m_pLock != NULL)
	{
		m_pLock->UnLock();
		m_pLock = NULL;
	}
}



